package com.my.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my.entity.WecomConfig;
import com.my.mapper.WecomConfigMapper;
import com.my.request.WecomConfigChangeReq;
import com.my.request.WecomConfigQueryReq;
import com.my.request.WecomConfigUpdateReq;
import com.my.response.WecomConfigDetailResponse;
import com.my.response.WecomConfigResponse;
import com.my.result.PageResult;
import com.my.service.WecomConfigService;
import com.my.util.PageResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sjx
 * @date 2025/2/13 10:11
 */
@Service
public class WecomConfigServiceImpl extends ServiceImpl<WecomConfigMapper, WecomConfig> implements WecomConfigService {
    @Override
    public PageResult<WecomConfigResponse> getPage(WecomConfigQueryReq query) {
        Page<WecomConfig> page = this.lambdaQuery()
                .like(StringUtils.isNotBlank(query.getConfigCode()),WecomConfig::getConfigCode,query.getConfigCode())
                .like(StringUtils.isNotBlank(query.getConfigName()),WecomConfig::getConfigName,query.getConfigName())
                .eq(query.getStatus() != null, WecomConfig::getStatus,query.getStatus())
                .ge(query.getBeginTime() != null,WecomConfig::getCreateTime,query.getBeginTime())
                .le(query.getEndTime() != null,WecomConfig::getCreateTime,query.getEndTime())
                .page(new Page<>(query.getCurrent(),query.getPageSize()));
        if(CollectionUtils.isEmpty(page.getRecords())){
            return PageResultUtil.buildPageInfo(new ArrayList<>(),page.getTotal());
        }

        List<WecomConfigResponse> responseList = new ArrayList<>();
        for(WecomConfig wecomConfig : page.getRecords()){
            WecomConfigResponse wecomConfigResponse = new WecomConfigResponse();
            wecomConfigResponse.setId(wecomConfig.getId());
            wecomConfigResponse.setConfigCode(wecomConfig.getConfigCode());
            wecomConfigResponse.setConfigName(wecomConfig.getConfigName());
            wecomConfigResponse.setStatus(wecomConfig.getStatus());
            wecomConfigResponse.setCreateTime(wecomConfig.getCreateTime());
            wecomConfigResponse.setRemark(wecomConfig.getRemark());
            responseList.add(wecomConfigResponse);
        }

        return PageResultUtil.buildPageInfo(responseList,page.getTotal());
    }

    @Override
    public WecomConfigDetailResponse getInfo(Long id) {
        WecomConfig wecomConfig = this.getById(id);
        if(wecomConfig == null){
            return null;
        }

        WecomConfigDetailResponse response = new WecomConfigDetailResponse();
        response.setId(wecomConfig.getId());
        response.setConfigCode(wecomConfig.getConfigCode());
        response.setConfigName(wecomConfig.getConfigName());
        response.setStatus(wecomConfig.getStatus());
        response.setConfigVal(wecomConfig.getConfigVal());
        response.setRemark(wecomConfig.getRemark());
        return response;
    }

    @Override
    public void updateInfo(WecomConfigUpdateReq request) {
        WecomConfig wecomConfig = new WecomConfig();
        wecomConfig.setId(request.getId());
        wecomConfig.setConfigVal(request.getConfigVal());
        this.updateById(wecomConfig);
    }

    @Override
    public void changeStatus(WecomConfigChangeReq request) {
        WecomConfig wecomConfig = new WecomConfig();
        wecomConfig.setId(request.getId());
        wecomConfig.setStatus(request.getStatus());
        this.updateById(wecomConfig);
    }
}
